package com.example.combwx.mvp.view.news;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.combwx.R;
import com.example.combwx.mvp.base.model.BaseEntity;
import com.example.combwx.mvp.base.view.BaseVpFragment;
import com.example.combwx.mvp.contract.LREContract;
import com.example.combwx.mvp.di.DaggerLREComponent;
import com.example.combwx.mvp.di.LREModules;
import com.example.combwx.mvp.model.HttpCode;
import com.example.combwx.mvp.model.entity.ImageBannerEntity;
import com.example.combwx.mvp.model.entity.NewsEntity;
import com.example.combwx.mvp.presenter.LREPresenter;
import com.example.combwx.mvp.view.news.adapter.ContentNewsAdapter;
import com.example.combwx.mvp.view.news.adapter.ImageBannerAdapter;
import com.example.combwx.utils.ToasetUtils;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContentNewsFragment extends BaseVpFragment<LREPresenter>
        implements LREContract.ILREViwe, OnRefreshLoadMoreListener,
        OnItemClickListener {

    private long channel_id;
    private SmartRefreshLayout contentNewsSmart;
    private RecyclerView contentNewsRv;
    private View headView;
    private ViewPager headVp;
    private ContentNewsAdapter adapter;
    private ImageBannerAdapter vpAdapter;

    public ContentNewsFragment(long chnnel_id) {
        this.channel_id = chnnel_id;
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_content_news;
    }

    @Override
    public void initView() {
        headView = LayoutInflater.from(getContext()).inflate(R.layout.view_news_banner, null);
        headVp = headView.findViewById(R.id.news_banner_vp);
        contentNewsSmart = (SmartRefreshLayout) f(R.id.content_news_smart);
        contentNewsSmart.setEnableRefresh(true);
        contentNewsSmart.setEnableLoadMore(true);
        contentNewsSmart.setRefreshHeader(new ClassicsHeader(getContext()));
        contentNewsSmart.setRefreshFooter(new ClassicsFooter(getContext()));
        contentNewsSmart.setOnRefreshLoadMoreListener(this);
        contentNewsRv = (RecyclerView) f(R.id.content_news_rv);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        contentNewsRv.setLayoutManager(manager);
        adapter = new ContentNewsAdapter();
        adapter.addHeaderView(headView);
        contentNewsRv.setAdapter(adapter);
        adapter.setOnItemClickListener(this);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("vptiao");
        VpProvider vpProvider = new VpProvider();
        getActivity().registerReceiver(vpProvider,intentFilter);
    }

    @Override
    public void initData() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", HttpCode.CHANNELNEWS);
        map.put("channel_id", 1);
        HashMap<String, Object> bannerMap = new HashMap<>();
        bannerMap.put("code", HttpCode.IMAGERBANNERCODE);
        p.requestAll(bannerMap, map);
    }

    @Override
    public void inject() {
        DaggerLREComponent.builder().lREModules(new LREModules(this))
                .build().injectContentNewsFragment(this);

    }

    @Override
    public void refreshAll(BaseEntity entity) {
        if (entity instanceof ImageBannerEntity) {
            ImageBannerEntity bannerEntity = (ImageBannerEntity) entity;
            vpAdapter = new ImageBannerAdapter(bannerEntity.getValues());
            headVp.setAdapter(vpAdapter);
        }
        if (entity instanceof NewsEntity) {
            NewsEntity newsEntity = (NewsEntity) entity;

            if (newsEntity.getValues() == null) {
                ToasetUtils.showMsg("最后一页");
                return;
            }
            if (newsEntity.getValues().size() == 0) {
                ToasetUtils.showMsg("最后一页");
                return;
            }
            adapter.addData(newsEntity.getValues());
        }

    }

    @Override
    public void refreshRecyckerView(BaseEntity entity) {
        NewsEntity newsEntity = (NewsEntity) entity;
        adapter.setNewInstance(newsEntity.getValues());
    }

    @Override
    public void loadMoreRecyclerView(BaseEntity entity) {
        NewsEntity newsEntity = (NewsEntity) entity;

        if (newsEntity.getValues() == null) {
            ToasetUtils.showMsg("最后一页");
            return;
        }

        if (newsEntity.getValues().size() == 0) {
            ToasetUtils.showMsg("最后一页");
            return;
        }

        List<NewsEntity.Values> list = adapter.getData();
        list.addAll(newsEntity.getValues());
        adapter.addData(list);

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishLoadMore();
        Map<String, Object> map = new HashMap<>();
        map.put("code", HttpCode.CHANNELNEWS);
        map.put("channel_id", channel_id);
        p.requestLoad(map);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        refreshLayout.finishRefresh();
        Map<String,Object> map = new HashMap<>();
        map.put("code",HttpCode.CHANNELNEWS);
        map.put("channel_id",channel_id);
        p.requestRefresh(map);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        NewsEntity.Values values = (NewsEntity.Values) adapter.getItem(position);
        String url = values.news_url;
        long id = values.news_id;
        Bundle bundle = new Bundle();
        bundle.putLong("news_id",id);
        bundle.putString("news_url",url);
        startPage(bundle,NewsValuesActivity.class);

    }
    class VpProvider extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals("vptiao")){
                Bundle bundle = new Bundle();
                bundle.putLong("news_id",1);
                bundle.putString("news_url","http://118.195.161.134:8066/index.html#");
                startPage(bundle, NewsValuesActivity.class);
            }
        }
    }
}
